//
//  BaseViewController.swift
//  ezyform
//
//  Created by FengMoennig on 1/12/16.
//  Copyright © 2016 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import CoreLocation

protocol ReloadDataDelegate: NSObjectProtocol{
    func reloadData()
}
class BaseViewController: UIViewController {
    var isViewDidLoad: Bool = false//用于表示是否调用了viewDidLoad，也就是注册了所有的观察者
    override func viewDidLoad() {
        super.viewDidLoad()
        //收得到Importent通知要隐藏键盘
        NotificationCenter.default.addObserver(self, selector: #selector(hideKeyBoard), name: UIResponder.keyboardWillHideNotification, object: nil)
        //back
        if self.navigationController != nil {
            if self.navigationController!.viewControllers.count > 1 {
                let btn = UIButton(type: .custom)
                btn.frame = CGRect(x: 0, y: 0, width: 30, height: 40)
                btn.setImage(UIImage(named: "service_navigationbar_back_icon"), for: .normal)
                btn.addTarget(self, action: #selector(backToPre), for: .touchUpInside)
                let item = UIBarButtonItem(customView: btn)
//                self.navigationItem.setLeftBarButtonItems([item], animated: true)
                self.navigationItem.leftBarButtonItem = item//这样做修复iOS11显示两个返回按钮的问题
            }
        }
        NotificationCenter.default.addObserver(self, selector: #selector(becomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(resignActive), name: UIApplication.willResignActiveNotification, object: nil)
        self.isViewDidLoad = true
    }
    @objc func backToPre(){
        _ = self.navigationController?.popViewController(animated: false)
    }
    
    func compareTime(){
        if SBTimeManager.shared.isOver()  {
            self.jumpToLogin()
        }
    }
    func jumpToLogin(){
        self.presentedViewController?.dismiss(animated: true, completion: nil)
        LoginService.logOut()
    }
    @objc func becomeActive(){
        self.compareTime()
    }
    @objc func resignActive(){
        SBTimeManager.shared.resetTime()
    }
    override var shouldAutorotate : Bool {
        return false
    }
    
    override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
    func setNavigationBarAppearence(_ showFlag: Bool){
        if let naviVC = self.navigationController{
            //不启用大标题
            naviVC.navigationBar.prefersLargeTitles = false
            //导航栏标题字体大小和颜色
            naviVC.navigationBar.isTranslucent = false
            //
            if #available(iOS 13.0, *) {
                let appearance = UINavigationBarAppearance()
                appearance.configureWithOpaqueBackground()
                appearance.backgroundColor = .white//导航栏背景颜色
                if showFlag {//显示底部灰线
                    appearance.shadowColor = UIColor(hexStr: Constant.SeparatorColorHexStr)//导航栏下边的灰色线
                }else{//不显示
                    appearance.shadowColor = .clear//隐藏导航栏下边的灰色线
                }
                //导航栏标题字体大小和颜色
                appearance.titleTextAttributes =  [.font: R.font.interSemiBold(size: 17 * Constant.currentSizeScale) as Any,.foregroundColor: UIColor.black ]
                naviVC.navigationBar.standardAppearance = appearance
                naviVC.navigationBar.scrollEdgeAppearance = naviVC.navigationBar.standardAppearance//处理iOS 15，导航栏默认为透明导致导航栏背景颜色不对的问题
            }else{
                naviVC.navigationBar.barTintColor = .white
                //导航栏标题字体大小和颜色
                naviVC.navigationBar.titleTextAttributes =  [.font: R.font.interSemiBold(size: 17 * Constant.currentSizeScale) as Any,.foregroundColor: UIColor.black ]
                if showFlag {//显示底部灰线
                    naviVC.navigationBar.shadowImage = nil
                }else{//不显示
                    naviVC.navigationBar.shadowImage = UIImage()//导航栏下边的灰线不显示
                }
                naviVC.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
            }
        }
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.compareTime()
        SBTimeManager.shared.resetTime()
        self.setNavigationBarAppearence(false)
    }
    deinit {
        DebugLog("\(self) deinit")
        NotificationCenter.default.removeObserver(self)//取消通知
    }
    @objc func hideKeyBoard(){
        
    }
}
